//
//  LYSettingViewController.m
//  artist
//
//  Created by Jay_Apple on 16/4/11.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYSettingViewController.h"
#import "LYSettingModel.h"

@interface LYSettingViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) NSMutableArray *modelsArray;


@property (weak, nonatomic) IBOutlet UIButton *loginOutBtn;

@end

@implementation LYSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"设置";
    
    [self setUpThemeNotification];
}


- (void)dealloc{

    [self resignThemeNotification];

}


- (void)upDateTheme{

    self.loginOutBtn.hidden = ![VVYCheckStatus sharedInstance].isLogin;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 懒加载

- (NSMutableArray *)modelsArray{
    
    if (_modelsArray == nil) {
        _modelsArray = [LYSettingModel mj_objectArrayWithFilename:@"setting.plist"];
    }
    
    return _modelsArray;
}


#pragma mark - tableViewDataSource
/**
 *  cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    LYSettingModel *model = self.modelsArray[indexPath.section][indexPath.row];
    
    
    cell.textLabel.text = model.title;
    cell.textLabel.font = [UIFont systemFontOfSize:LYDiscoveryCellTextFont];
    cell.textLabel.textColor = [UIColor colorWithHexString:LYDiscoveryCellTextColor alpha:1.0];
    
    cell.detailTextLabel.text = model.content;
    cell.detailTextLabel.font = [UIFont systemFontOfSize:LYDiscoveryCellTextFont];
    cell.detailTextLabel.textColor = [UIColor colorWithHexString:LYDiscoveryCellContentTextColor alpha:1.0];
    
    
    if (model.accesory) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
            cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return LYDiscoveryCellHeight;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.modelsArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.modelsArray[section] count];
}


/**
 *  secitonHeader
 */

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return LYMineSectionHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    LYSettingModel *model = self.modelsArray[indexPath.section][indexPath.row];
    
    if ([model.title containsString:@"缓存"]) {
        
        [self clearCaches];
        
    }else if ([model.title containsString:@"联系"]){
        
        
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",model.content];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

        
    
    }else if ([model.title containsString:@"版本"]){
    
        [MBProgressHUD showOnlyText:@"当前已是最新版本"];
    }else{
    
        Class pushClass = NSClassFromString(model.classStr);
        UIViewController *vc = [[pushClass alloc] init];
        vc.navigationItem.title = model.title;
        [self.navigationController pushViewController:vc animated:YES];

    
    }
    
}



#pragma mark - 按钮点击

- (IBAction)loginOutBtnOnclik:(id)sender {
    
    [[VVYCheckStatus sharedInstance] loginOut];
    [MBProgressHUD showSuccess:@"退出成功"];
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 其他
- (void)clearCaches{
    
    
    float tmpSize = [[SDImageCache sharedImageCache] getSize] / (1024 * 1024);
    
    NSString *clearCacheName = tmpSize >= 1 ? [NSString stringWithFormat:@"清理缓存(%.2fM)",tmpSize] : [NSString stringWithFormat:@"清理缓存(%.2fK)",tmpSize * 1024];
    //答题完毕
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清除缓存" message:clearCacheName preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[SDImageCache sharedImageCache] clearDisk];
        [MBProgressHUD showSuccess:@"清除成功"];
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    

}

@end
